package com.qingke.command;

import java.util.HashMap;
import java.util.Map;

import com.qingke.command.impl.AddCommand;
import com.qingke.command.impl.HelpCommand;
import com.qingke.command.impl.SelectCommand;
import com.qingke.command.impl.UpdateCommand;

public class CommandFactory {
	public static enum CommandCode{
		ADD,UPDATE,SELECT,HELP
	}
	
	private static Map<CommandCode,Class<? extends Command>> commandMap = new HashMap<>();
	
	static{
		commandMap.put(CommandCode.HELP, HelpCommand.class);
		commandMap.put(CommandCode.ADD, AddCommand.class);
		commandMap.put(CommandCode.SELECT, SelectCommand.class);
		commandMap.put(CommandCode.UPDATE, UpdateCommand.class);
		
	}
	
	public static Command buildCommand(CommandCode cmdCode){
		Class<? extends Command> cmdClass=commandMap.get(cmdCode);
		if(cmdClass!=null){
			try {
				return cmdClass.newInstance();
			} catch (InstantiationException | IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return null;
	}
}
